home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Source Sea20778672001.psc / clsDictionary.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-04-01  |  3.7 KB  |  163 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsDictionary"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'The collection of definitions
  17. Private m_colDefinition As Collection
  18.  
  19. Public Event ItemAdded(NewItem As clsDefinition)
  20. Public Event ItemRemoved(ItemKey As String)
  21. Public Event ItemUpdated(ChangedItem As clsDefinition)
  22. Public Event ItemDoesNotExist(vItemKey As Variant)
  23.  
  24. 'Returns the number of definitions
  25. Public Property Get Count() As Long
  26.  
  27.   Count = m_colDefinition.Count
  28.  
  29. End Property
  30.  
  31. 'Method to trigger the update event
  32. Public Function Update(ByVal v_vntIndex As Variant)
  33.  
  34.   Dim tItem As clsDefinition
  35.   Dim tKey As String
  36.   
  37.   'Gets the key (if the index was submitted
  38.   tKey = GetKey(v_vntIndex)
  39.   
  40.   'If the key does not exist then exit method
  41.   If tKey = "" Then Exit Function
  42.   
  43.   'Setup the new item
  44.   Set tItem = m_colDefinition.Item(tKey)
  45.   
  46.   'Raise the event
  47.   RaiseEvent ItemUpdated(tItem)
  48.  
  49. End Function
  50.  
  51. 'Method to add a new definition
  52. Public Function Add(ByVal v_strKey As String, Optional ByVal v_strPhrase As String = "", Optional ByVal v_intSeverity As Integer = 1, Optional ByVal v_strDescription As String = "") As Boolean
  53.   
  54.   Dim objDefinition As New clsDefinition
  55.   Dim x As Integer
  56.   
  57.   On Error Resume Next
  58.   
  59.   'Setup each property of the definistion
  60.   objDefinition.Key = v_strKey
  61.   objDefinition.Phrase = v_strPhrase
  62.   objDefinition.Description = v_strDescription
  63.   objDefinition.Severity = v_intSeverity
  64.   
  65.   'Add the definition
  66.   m_colDefinition.Add objDefinition, v_strKey
  67.   
  68.   'If there has been an error, return false, otherwise true
  69.   If Err.Number <> 0 Then
  70.     Add = False
  71.   Else
  72.     Add = True
  73.   End If
  74.   
  75.   'Raise the item added event
  76.   RaiseEvent ItemAdded(objDefinition)
  77.   
  78.   Set objDefinition = Nothing
  79.  
  80. End Function
  81.  
  82. 'Method to clear the definitions
  83. Public Sub Clear()
  84.  
  85.   Dim x As Integer
  86.   
  87.   'Remove each definition
  88.   For x = m_colDefinition.Count To 1 Step -1
  89.     m_colDefinition.Remove x
  90.   Next x
  91.   
  92. End Sub
  93.  
  94. 'Method to remove a single definition
  95. Public Function Remove(ByVal v_vntIndex As Variant)
  96.  
  97.   Dim tKey As String
  98.   
  99.   On Error Resume Next
  100.   
  101.   'Get the key of the definition if it's index was passed
  102.   tKey = GetKey(v_vntIndex)
  103.   
  104.   'If the key does not exist then get out
  105.   If tKey = "" Then Exit Function
  106.   
  107.   'Remove the definition
  108.   m_colDefinition.Remove v_vntIndex
  109.   
  110.   'Raise the event
  111.   RaiseEvent ItemRemoved(tKey)
  112.   
  113. End Function
  114.  
  115. 'Method to return an item
  116. Public Function Item(ByVal v_vntIndex As Variant) As clsDefinition
  117.  
  118.   On Error Resume Next
  119.   'Get the item from the collection
  120.   Set Item = m_colDefinition.Item(v_vntIndex)
  121.   
  122.   'If there was an error, handle it
  123.   If Err.Number <> 0 Then
  124.     RaiseEvent ItemDoesNotExist(v_vntIndex)
  125.     Set Item = Nothing
  126.   End If
  127.   
  128. End Function
  129.  
  130. 'Verifies and returns a key to a definition
  131. Private Function GetKey(vIndex As Variant) As String
  132.  
  133.   Dim tItem As clsDefinition
  134.   
  135.   'Attemp to set the item by the key
  136.   Set tItem = m_colDefinition.Item(vIndex)
  137.   
  138.   'If there was an error, raise the event
  139.   If Err.Number <> 0 Then
  140.     RaiseEvent ItemDoesNotExist(vIndex)
  141.     GetKey = ""
  142.   Else
  143.     'Rerturn the items key
  144.     GetKey = tItem.Key
  145.   End If
  146.  
  147.   Set tItem = Nothing
  148.  
  149. End Function
  150.  
  151. 'Triggered on class initialize
  152. Private Sub Class_Initialize()
  153.  
  154.   'Initializes the collection
  155.   Set m_colDefinition = New Collection
  156.   
  157. End Sub
  158.  
  159.  
  160.  
  161.  
  162.  
  163.